package com.isoft.airportsystem.controller.change;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.airportsystem.common.change.Result;
import com.isoft.airportsystem.entity.change.FlightChangeHistory;
import com.isoft.airportsystem.entity.change.FlightChangeRequest;
import com.isoft.airportsystem.entity.dao.FlightChangeRequestDTO;
import com.isoft.airportsystem.entity.dao.PageQuery;
import com.isoft.airportsystem.service.change.FlightChangeService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/flight-change")
@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:3000", "http://localhost:5173"}, allowCredentials = "true")
public class FlightChangeController {   // 变更管理

    private final FlightChangeService flightChangeService;

    public FlightChangeController(FlightChangeService flightChangeService) {
        this.flightChangeService = flightChangeService;
    }

    @PostMapping("/request")   // 提交变更请求
    public Result<FlightChangeRequest> submitChangeRequest(@RequestBody FlightChangeRequestDTO dto) {
        return flightChangeService.submitChangeRequest(dto);
    }

    @PostMapping("/review/{requestId}")   // 审核 变更请求
    public Result<FlightChangeRequest> reviewChangeRequest(@PathVariable String requestId,
                                                           @RequestParam String status,
                                                           @RequestParam(required = false) String comment,
                                                           @RequestParam String reviewerId) {
        return flightChangeService.reviewChangeRequest(requestId, status, comment, reviewerId);
    }

    @GetMapping("/requests")    // 获取 变更请求
    public Page<FlightChangeRequest> getChangeRequests(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String flightId,
            @RequestParam(required = false) String status) {
        System.out.println("=== 变更申请分页请求 ===");
        System.out.println("页码: " + page);
        System.out.println("每页大小: " + size);
        System.out.println("航班号: " + flightId);
        System.out.println("状态: " + status);
        System.out.println("========================");

        PageQuery query = new PageQuery(page, size);
        Page<FlightChangeRequest> result = flightChangeService.getChangeRequests(query, flightId, status);

        System.out.println("=== 变更申请分页响应 ===");
        System.out.println("总记录数: " + result.getTotal());
        System.out.println("当前页记录数: " + result.getRecords().size());
        System.out.println("总页数: " + result.getPages());
        System.out.println("当前页: " + result.getCurrent());
        System.out.println("每页大小: " + result.getSize());
        System.out.println("========================");

        return result;
    }

    @GetMapping("/history")   // 获取 变更记录
    public Page<FlightChangeHistory> getChangeHistory(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String flightId,
            @RequestParam(required = false) String userId) {
        System.out.println("=== 变更历史分页请求 ===");
        System.out.println("页码: " + page);
        System.out.println("每页大小: " + size);
        System.out.println("航班号: " + flightId);
        System.out.println("用户ID: " + userId);
        System.out.println("========================");

        PageQuery query = new PageQuery(page, size);
        Page<FlightChangeHistory> result = flightChangeService.getChangeHistory(query, flightId, userId);

        System.out.println("=== 变更历史分页响应 ===");
        System.out.println("总记录数: " + result.getTotal());
        System.out.println("当前页记录数: " + result.getRecords().size());
        System.out.println("总页数: " + result.getPages());
        System.out.println("当前页: " + result.getCurrent());
        System.out.println("每页大小: " + result.getSize());
        System.out.println("========================");

        return result;
    }
}